package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * springmvc 默认情况下封装参数使用的是QueryString(/xxx?xxx):@RequestParam
     * 如果参数是其他形式，比如json请求体中，那么需要加上@RequestBody
     *
     * @param params 分页参数
     * @param model  查询模型条件
     * @author jin
     * @date 2021/3/28 19:06
     */
    @Override
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {

        //通过工具类获得companyId
        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVO = courseBaseService.queryCourseList(params, model, companyId);
        return pageVO;
    }



    /**
     * 保存课程基本信息，保存到数据库后回显给前端
     * @param baseVo 前端传输的课程的基本信息
     * @author jin
     * @date 2021/3/30 19:30
     */
    @Override
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVo baseVo){
        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO baseDTO = CourseBaseConvert.INSTANCE.courseBaseVoToDTo(baseVo);
        baseDTO.setCompanyId(companyId);
        return courseBaseService.createCourseBase(baseDTO);
    }




    @Override
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getByCourseId(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return this.courseBaseService.getByCourseId(courseBaseId,companyId);
    }



    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVo courseBaseVo) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.courseBaseVoToDTo(courseBaseVo);
        courseBaseDTO.setCompanyId(SecurityUtil.getCompanyId());
        return this.courseBaseService.modifyCourseBase(courseBaseDTO);
    }
}
